#include "prop.h"
#include <stdlib.h>
#include <stdio.h>

prop *CreateProp(enum PropType type, void *ptr)
{
    prop *p = (prop*)malloc(sizeof(prop));
    if(p == NULL)
    {
        return NULL;
    }
    p->type = type;
    p->ptr = ptr;
    return p;
}

void FreeProp(void *p, void (*ClearPtr)(void *))
{
    prop *pro = (prop*)p;
    ClearPtr(pro->ptr);
    free(pro);
}

weapon *CreateWeapon(const char *name, int ATK, enum Effect effect,int count)
{
    weapon *w = (weapon*)malloc(sizeof(weapon));
    if(w == NULL)
    {
        return NULL;
    }
    w->count = count;
    w->Maxcount = count;
    // w->id = id;
    w->ATK = ATK;
    w->effect = effect;
    w->name = GetString(name);
    return w;
}

void FreeWeapon(void *ptr)
{
    weapon *w = (weapon*)ptr;
    ClearString(&w->name);
    free(w);
}


MyStr WeaponToString(weapon *w)
{
    char str[1024] = {0};
    sprintf(str,"%d %s %d %d %d\n",Weapon,w->name.string,w->ATK,w->effect,w->count);
    return GetString(str);
}

defence *CreateDefence(const char *name,int DEF,enum Effect effect,int count)
{
    defence *d = (defence*)malloc(sizeof(defence));
    if(d == NULL)
    {
        return NULL;
    }
    // d->id = id;
    d->count = count;
    d->Maxcount = count;
    d->effect = effect;
    d->DEF = DEF;
    d->name = GetString(name);
    return d;
}

MyStr DefenceToString(defence *d)
{
    char str[1024] = {0};
    sprintf(str,"%d %s %d %d %d\n",Defence,d->name.string,d->DEF,d->effect,d->count);
    return GetString(str);
}

void FreeDefence(void *ptr)
{
    defence *d = (defence*)ptr;
    ClearString(&d->name);
    free(d);
}

medicament *CreateMedicament(const char *name, int HP,enum Effect effect)
{
    medicament *m = (medicament*)malloc(sizeof(medicament));
    if(m == NULL)
    {
        return NULL;
    }
    m->effect = effect;
    m->HP = HP;
    m->name = GetString(name);
    // m->id = id;
    return m;
}

MyStr MedicamentToString(medicament *m)
{
    char str[1024] = {0};
    sprintf(str,"%d %s %d %d\n",Medicament,m->name.string,m->HP,m->effect);
    return GetString(str);
}

void FreeMedicament(void *ptr)
{
    medicament *m = (medicament*)ptr;
    ClearString(&m->name);
    free(m);
}



// void InitWeapon(weapon *w)
// {
//     w->ATK = 0;
//     w->effect = -1;
//     w->id = 0;
//     w->name.string = NULL;
// }

// void InitDefence(defence *d)
// {
//     d->def = 0;
//     d->effect = -1;
//     d->id = 0;
//     d->name.string = NULL;
// }

// void InitMedicament(medicament *m)
// {
//     m->effect = -1;
//     m->HP = 0;
//     m->id = 0;
//     m->name.string = NULL;
//     m->count = 0;
// }
